package com.xwm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * @author xwm
 * @email 2825902051@qq.com
 * @date 2021/6/26 0026
 * @time 22:49
 */

@Component
public class MailConfig {
    @Autowired
    JavaMailSenderImpl javaMailSenderl;
    @Autowired
    TemplateEngine engine;

    @Value("${qq-email.from}")
    private String from;
    @Value("${qq-email.to}")
    private String to;

    //发送一个简单邮件
    public void sendMail(){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setBcc();
        mailMessage.setFrom("1769293942@qq.com");
        mailMessage.setTo("2825902051@qq.com");
        mailMessage.setSentDate(new Date());
        mailMessage.setSubject("test");
        mailMessage.setText("hello springboot email");
        javaMailSenderl.send(mailMessage);
    }

    //发送一个包含附件（资源）的邮件
    public void sendMailAndSource() throws MessagingException {


        File file = new File("D:\\爬虫练习\\热门图片\\0c0d4cee-6678-4c07-a9f1-458a48b6c644.jpg");
        String name = file.getName();
        MimeMessage mimeMessage = javaMailSenderl.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSentDate(new Date());
        mimeMessageHelper.setSubject("test1");
        mimeMessageHelper.setText("<a href='http://www.baidu.com'>点击验证人机身份</a>",true);
        mimeMessageHelper.addAttachment(name,file);
        javaMailSenderl.send(mimeMessage);
    }

    //发送一个包含附件（资源）的邮件
    public void sendMailSourceInHtml() throws MessagingException {


        File file = new File("D:\\爬虫练习\\热门图片\\0c0d4cee-6678-4c07-a9f1-458a48b6c644.jpg");
        String name = file.getName();
        MimeMessage mimeMessage = javaMailSenderl.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSentDate(new Date());
        mimeMessageHelper.setSubject("test1");
        mimeMessageHelper.setText("<h1>这是一封带有图片资源的邮件</h1>"+"<div>图片1：<img src='cid:01'/></div>",true);
        mimeMessageHelper.addAttachment(name,file);
        mimeMessageHelper.addInline("01",file);
        javaMailSenderl.send(mimeMessage);
    }

    //发送一个页面是由thymeleaf模板构成的邮件内容

    public void sendMailContentHtml() throws MessagingException {
        Context context=new Context();
context.setVariable("name","张三");
context.setVariable("pwd","4fsdfd54");
        String s = engine.process("test.html", context);
        File file = new File("D:\\爬虫练习\\热门图片\\0c0d4cee-6678-4c07-a9f1-458a48b6c644.jpg");
        String name = file.getName();
        MimeMessage mimeMessage = javaMailSenderl.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSentDate(new Date());
        mimeMessageHelper.setSubject("test1");
        mimeMessageHelper.setText(s,true);
        mimeMessageHelper.addAttachment(name,file);

        javaMailSenderl.send(mimeMessage);
    }

}
